﻿using LNet;
using OestsCommon.Model;
using OestsCommon.Player;
using OestsServer.Net;
using System.Collections.Generic;
public class RoleData
{
    public string Name;
    public ClientChannel client;
    public Point3 position;

    public RoleData()
    {

    }

    public RoleData(string name, ClientChannel c, Point3 pos)
    {
        Name = name;
        client = c;
        position = pos;
    }

    public RoleData(Struct2<string, Point3> data, ClientChannel c)
    {
        Name = data.Value1;
        position = data.Value2;
        client = c;
    }
}
namespace OestsServer.Handlers.UnityHandlers
{
    public class RoleHandler : HandlerBase<UnityClientChannel>
    {


        private static List<RoleData> list = new List<RoleData>();
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.Role;
            }
        }

        public override void Execute(UnityClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            if (packet.RequestSubCode == SubCode.Role_Join)
            {
                Join(client, packet);
            }
            else if (packet.RequestSubCode == SubCode.Role_SetDestination)
            {
                Setdestination(client, packet);
            }
            else if (packet.RequestSubCode == SubCode.Role_SyncPosition)
            {
                SyncPostion(client, packet);
            }
        }

        public void Join(UnityClientChannel client, Request packet)
        {
            //接受到的数据
            Struct2<string, Point3> data = LitJson.JsonMapper.ToObject<Struct2<string, Point3>>(packet.DataStr);

            #region 将加入信息发送给已经存在的用户 对象为列表
            ResponsePacket = new Response(packet)
            {
                ReturnCode = ReturnCode.Success
            };
            List<Struct2<string, Point3>> list1 = new List<Struct2<string, Point3>>
            {
                data
            };
            ResponsePacket.DataObject = list1;
            foreach (var rd in list)
            {
                if (rd.Name != data.Value1)
                    rd.client.Send(ResponsePacket);
            }
            #endregion

            #region 将已经存在的用户发送给请求者
            Response res = new Response(PacketKey.Role, SubCode.Role_Join);
            List<Struct2<string, Point3>> list2 = new List<Struct2<string, Point3>>();
            //暂时测试用
            foreach (var rd in list)
            {
                list2.Add(new Struct2<string, Point3>(rd.Name, rd.position));
            }

            #endregion
            res.DataObject = list2;
            client.Send(res);
            list.Add(new RoleData(data, client));
        }

        public void SyncPostion(UnityClientChannel client, Request packet)
        {
            //RoleSyncData data = LitJson.JsonMapper.ToObject<RoleSyncData>(packet.DataStr);
            ResponsePacket = new Response(packet);
            if (client.User == null) return;
            Student student = client.User as Student;
            if(student != null)
            {
                //得到房间Id
                if (student != null)
                {
                    //  Student s = client.User as Student;
                    if (student._room != null)
                    {
                        UnityClientChannel.Signal_Project_Room_Except.Dispatch(client.ProjectType, student._room.RoomId, student.DBid, ResponsePacket);
                    }
                }
            }
        }

        public void Setdestination(UnityClientChannel client, Request packet)
        {
           // RoleSyncData data = LitJson.JsonMapper.ToObject<RoleSyncData>(packet.DataStr);
            ResponsePacket = new Response(packet);
            if (client.User == null) return;
            Student student = client.User as Student;
            //得到房间Id
            if (student != null)
            {
                //  Student s = client.User as Student;
                if (student._room != null)
                {
                    UnityClientChannel.Signal_Project_Room_Except.Dispatch(client.ProjectType, student._room.RoomId, student.DBid, ResponsePacket);
                }
            }

        }
    }
}
